﻿using System;
using weka.core;
using weka.core.converters;
using java.io;
using weka.filters.unsupervised.attribute;
using weka.filters;

namespace OMC.Classification
{
    /// <summary>
    /// an object of this class can manage all the parts of writing data to a file
    /// </summary>
    /// <remarks>
    /// Copyright 2012 Stefan Lütolf
    /// </remarks>
    public class DataWriter
    {
        private Instances TheRawData;
        private Instances TheFilteredRawData;
        private String TheFileDestination;
        private StringToWordVector TheUsedFilter = new StringToWordVector();
        /// <summary>
        /// set,get DataOutput format,
        /// false,if Data output is default and
        /// true, if data output is filtered
        /// </summary>
        public bool DataOutput { set; get; }
        private const bool isFiltered = true;

        /// <summary>
        /// the constructor to generate an object
        /// </summary>
        /// <param name="inData">Instances which contain the data</param>
        /// <param name="inArffDest">
        /// the file Path inkl. filename of the arff data file
        /// for example: -> C:\Program Files\Microsoft Office\Office14\BAMailClassData\
        /// if you chose the folowing string as inFilePath: "./BAMailClassData/ARFF/data.arff"
        /// </param>
        public DataWriter(Instances inData, String inArffDest)
        {
            this.TheFileDestination = inArffDest;
            this.TheRawData = inData;
        }

        /// <summary>
        /// this method generate the data and write it to the ARFF File
        /// </summary>
        public void make()
        {
            if (DataOutput == isFiltered)
            {
                TheUsedFilter.setInputFormat(TheRawData);
                TheFilteredRawData = Filter.useFilter(TheRawData, TheUsedFilter);
            }
            //Arff Saver
            ArffSaver TheArffSaver = new ArffSaver();
            try
            {
                TheArffSaver.setInstances(TheRawData);
                TheArffSaver.setFile(new File(TheFileDestination));
                TheArffSaver.writeBatch();
            }
            catch (IOException e)
            {
                e.StackTrace.ToString();
            }
        }
    }
}